//
//  RJLineView.m
//  AirQuality
//
//  Created by Po on 15/3/24.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJLineView.h"
#import "RJLine.h"
#import "AppDelegate.h"

@interface RJLineView ()
@property (retain, nonatomic) RJLine * lineView;                //折线图数据
@property (retain, nonatomic) NSDictionary * data;              //数据
@property (assign, nonatomic) CGFloat maxHeight;                //最大值
@property (assign, nonatomic) NSInteger yLabelWidth;            //纵轴宽度
@property (assign, nonatomic) BOOL isCO;
@property (strong, nonatomic) NSMutableArray    * allArray;

- (void)initInterface;
@end

@implementation RJLineView

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data timeArray:(NSArray *)time
{
    self = [super initWithFrame:frame];
    if (self) {
        _allArray = [NSMutableArray array];
        if (data) {
            NSArray * timeArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"00",];
            NSString * key = [data allKeys][0];
            NSDictionary * dataDic = [NSMutableDictionary dictionary];
            NSArray * tempArray = [[data allValues] lastObject];
            if (tempArray.count != 0) {
                for (NSDictionary * temp in tempArray) {
                    int i = 0;
                    NSMutableArray * array = [temp allValues][0];
                    NSMutableArray * tagArray = [NSMutableArray array];
                    if (time.count == timeArray.count) {
                        tagArray = array;
                        
                    }else {
                        for (NSString * tag in timeArray) {
                            if ([time indexOfObject:tag] == NSNotFound) {
                                [tagArray addObject:@"0"];
                            }else{
                                [tagArray addObject:array[i]];
                                 i ++;
                            } 
                        }
 
                    }
                    dataDic = @{[[temp allKeys] lastObject]:tagArray};
//                     [dataDic setObject:tagArray forKey:[[temp allKeys] lastObject]];
                    [_allArray addObject:dataDic];
                    
                }
                NSArray * staionCodes = [dataDic allKeys];
//                staionCodes = [staionCodes sortedArrayUsingSelector:@selector(compare:)];
                NSMutableArray * finalArray = [NSMutableArray array];
                NSMutableDictionary * lineDic = [NSMutableDictionary dictionary];
                for (NSString * key in staionCodes) {
                    [finalArray addObject:@{key:dataDic[key]}];
                    [lineDic setObject:key forKey:dataDic];
                }
//                NSString *timeKey = [[_data allKeys] firstObject];
                [lineDic setObject:_allArray forKey:key];
                _data = lineDic;
                _isCO = [key isEqualToString:@"CO"];
            }
            
        }
       
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    [self addSubview:[self getLineView]];
}

- (RJLine *)getLineView
{
    if (!_lineView) {
        CGRect rect = self.bounds;
        _yLabelWidth = DeviceType >= 6 ? 50 : 40;
        rect.origin.x += _yLabelWidth;
        rect.size.width -= _yLabelWidth;
        _maxHeight = [self getMaxHeightValue];
        if (_data.count != 0) {
            [self initYaxis];
        }
        _lineView = [[RJLine alloc] initWithFrame:rect data:_data maxHeight:_maxHeight];
    }
    return _lineView;
}

/**初始化Y轴*/
- (void)initYaxis
{
    CGFloat height = (self.bounds.size.height - 60) / 6;
    CGFloat labelHeight = height / 4 * 3;
//    CGFloat labelTopMargin = height / 4;
    NSInteger value = 7;
    CGRect rect = CGRectMake1(0,0, _yLabelWidth, labelHeight);
    for (NSUInteger i = 0; i < value; i ++) {
        if (DeviceType >= 6) {
        rect.origin.y = height * (i + 1) - height / 2-3;
        }else{
         rect.origin.y = height * (i + 1) - height / 2+7;
        }
        if (_isCO) {
            CGFloat unit = _maxHeight / (value - 1);               //平均每一段的值
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%.2f",(unit  * (value - 1 - i))]]];
            [self addSubview:[self getLabelWithFrame:CGRectMake1(0,0, _yLabelWidth, height / 2 + 5) title:@"(mg)"]];
        } else {
            NSUInteger unit = _maxHeight / (value - 1);               //平均每一段的值
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%@",@(unit * (value - 1- i))]]];
            [self addSubview:[self getLabelWithFrame:CGRectMake1(0,0, _yLabelWidth, height / 2 + 5) title:@"(ug)"]];
        }
    }
}

- (UILabel *)getLabelWithFrame:(CGRect)rect title:(NSString *)title
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor blackColor]];
    if (DeviceType >= 6) {
        [label setFont:[UIFont systemFontOfSize:rect.size.height-14]];
    }else{
        [label setFont:[UIFont systemFontOfSize:rect.size.height-3]];
    }
    return label;
}

- (CGFloat)getMaxHeightValue
{
    if (_data && _data.count != 0) {
        return [self getMaxNumberLine];
    }
    return 0;
}

- (CGFloat)getMaxNumberLine
{
    NSArray * allValue = [NSArray array];
    if ([[[_data allValues] firstObject] isKindOfClass:[NSString class]]) {
        allValue = [[_data allValues] lastObject];
    }else{
        allValue = [[_data allValues] firstObject];
    }
    if (allValue.count == 0) {
        return 0;
    }
    CGFloat maxNum = 0;
    for (NSDictionary * dic in allValue) {
        for (NSString * num in [[dic allValues] lastObject]) {
            CGFloat numFloat = [num floatValue];
            if (numFloat > maxNum) {
                maxNum = numFloat;
            }
        }
    }
    
    //计算最贴近哪条线
    if (_isCO) {
        return ((NSUInteger)maxNum + 1);
    } else {
        return [self getMaxLineNumWithNum:maxNum];
    }
    return 0;
}

/**非CO的最大值计算*/

- (CGFloat)getMaxLineNumWithNum:(CGFloat)num
{
//    NSUInteger temp = (NSUInteger)(num) + 50;
//    if ((temp % 50) > 25) {
//        return ((temp / 50) + 1) * 50;
//    } else {
//        return (temp / 50) * 50;
//    }
    return num + 10;
}

CG_INLINE CGRect
CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGRect rect;
    rect.origin.x = x * myDelegate.autoSizeScaleX; rect.origin.y = y * myDelegate.autoSizeScaleY;
    rect.size.width = width * myDelegate.autoSizeScaleX; rect.size.height = height * myDelegate.autoSizeScaleY;
    return rect;
}
@end
